// 检查是否登录
function isLogin() {
    if (JSON.parse(localStorage.getItem("is")).isLogin === "true")
        window.location.href = '../html/index.html';
}

// 设置登录
function setLogin() {
    let username = document.getElementById("username").value;
    let is = JSON.stringify({"username": username, "isLogin": "true"});
    localStorage.removeItem("is");
    localStorage.setItem("is", is);
    isLogin();
}

// 检验输入
// eslint-disable-next-line no-unused-vars
function check() {

    // 获取
    let username = document.getElementById("username").value;
    let password = document.getElementById("password").value;

    // 检验用户名
    if (username === "" || username == null) {
        printError("usernameErr", "请输入用户名");
        return false;
    } else if (localStorage.getItem(username) == null) {
        printError("usernameErr", "查无此账号");
        return false;
    } else printError("usernameErr", "&nbsp;");

    // 检验密码
    if (password === "" || password == null) {
        printError("passwordErr", "请输入密码");
        return false;
    } else if (JSON.parse(localStorage.getItem(username)).password !== password) {
        printError("passwordErr", "账号或密码错误");
        return false;
    } else printError("passwordErr", "&nbsp;");

    alert("登陆成功😉，" + getTimeGreeting() + username + " ！");
    setLogin();
}


// 显示错误消息
function printError(elemId, hintMsg) {
    document.getElementById(elemId).innerHTML = hintMsg;
}

// 忘记密码
// eslint-disable-next-line no-unused-vars
function forget() {
    alert("忘记密码？那就寄！");
}

// 帮助弹窗
// eslint-disable-next-line no-unused-vars
function help() {
    alert("\nWhat are you looking for?\n\nI can't help you either.🤷");
}

// 时间
let getTimeGreeting = () => {
    // 获取时间
    let timeNow = new Date();
    let hours = timeNow.getHours();
    let text;
    // 判断
    if (hours >= 0 && hours <= 5) {
        text = "凌晨好啊";
    } else if (hours > 5 && hours <= 8) {
        text = "早上好啊";
    } else if (hours > 8 && hours <= 11) {
        text = "上午好啊";
    } else if (hours > 11 && hours <= 14) {
        text = "中午好啊";
    } else if (hours > 14 && hours <= 18) {
        text = "下午好啊";
    } else if (hours > 18 && hours <= 24) {
        text = "晚上好啊";
    } else {
        text = "你究竟在什么时间段里？";
    }
    // 返回状态
    return text;
};